package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow

object Demo2CountWindow {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val wordsDS: DataStream[String] = linesDS.flatMap(_.split(','))

    val kvDS: DataStream[(String, Int)] = wordsDS.map((_, 1))

    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    /**
     * 统计窗口
     * countWindow(10): 滚动的统计窗口
     * countWindow(10,5)： 滑动的统计窗口
     *
     */

    val countWindowDS: WindowedStream[(String, Int), String, GlobalWindow] = keyByDS
      .countWindow(10, 5)


    val countDS: DataStream[(String, Int)] = countWindowDS.sum(1)

    countDS.print()

    env.execute()

  }

}
